package C9_1_DecomposeConditional;


/**
 * @author xugang1
 * @updated 2022/7/25
 * @description 分解条件表达式
 */
public class DecomposeConditional {

    /**
     * 重构前
     * 计算价格：冬季和夏季是不同的
     */
    public void test1(){
        if(date.before(SUMMER_START)||date.after(SUMMER_END))
            charge = quantity * _winterRate + _winnerServiceCharge;
        else charge = quantity * _summerRate;
    }


    /**
     * 重构后
     * 条件和代码块提炼为独立的函数
     * 可读性提高，独立函数功能表达清楚
     */
    public void test2(){
        if(notSummer(date))
            charge = winterCharge(quantity);
        else charge = summerCharge(quantity);
    }

    private boolean notSummer(Date date){
        return date.before(SUMMER_START) || date.after(SUMMER_END);
    }
    private double summerCharge(int quantity){
        return quantity * _summerRate;
    }
    private double winterCharge(int quantity){
        return quantity * _winterRate + _winnerServiceCharge;
    }
}